package cn.hncu.booksManagementSystem.in.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.in.business.ebi.InDetailEbi;
import cn.hncu.booksManagementSystem.in.business.factory.InDetailEbiFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import cn.hncu.booksManagementSystem.utils.DateUtil;
import javax.swing.JScrollPane;

public class DetailPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JFrame mainFrame;
	private InMainModel inMain;
	private JList<InDetailModel> jList;
	private InDetailQueryModel idqm = null;
	//注入ebi
	InDetailEbi inDetailEbi = InDetailEbiFactory.getInstanceOfInDetailEbi();
	UserEbi userEbi = UserEbiFactory.getInstanceOfUserEbi();
	/**
	 * Create the panel.
	 * @param idqm 
	 */
	public DetailPanel(JFrame mainFrame,InMainModel inMain,InDetailQueryModel idqm) {
		this.mainFrame = mainFrame;
		this.inMain = inMain;
		this.idqm = idqm;
		initContentPane();
		initData();
	}
	
	private void initData() {
		InDetailQueryModel idqm2 = new InDetailQueryModel();
		idqm2.setInId(inMain.getId());
		if (idqm!=null) {
			idqm2.setId(idqm.getId());
			idqm2.setBookId(idqm.getBookId());
			idqm2.setSumNum(idqm.getSumNum());
			idqm2.setSumNum2(idqm.getSumNum2());
			idqm2.setSumMoney(idqm.getSumMoney());
			idqm2.setSumMoney2(idqm.getSumMoney2());
		}
		Collection<InDetailModel> details = inDetailEbi.getInDetailsByCondition(idqm2);
		
		jList.setListData(details.toArray(new InDetailModel[0]));
	}

	private void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("订单");
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setBounds(317, 39, 157, 38);
		add(label);
		
		JLabel lblNewLabel = new JLabel("订单编号：");
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel.setBounds(70, 107, 86, 38);
		add(lblNewLabel);
		
		JLabel labInMainId = new JLabel(inMain.getId());
		labInMainId.setHorizontalAlignment(SwingConstants.LEFT);
		labInMainId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		labInMainId.setBounds(150, 107, 105, 38);
		add(labInMainId);
		
		JLabel label_2 = new JLabel("日期：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_2.setBounds(238, 107, 86, 38);
		add(label_2);
		
		JLabel labDate = new JLabel(DateUtil.long2String(inMain.getInDate()));
		labDate.setHorizontalAlignment(SwingConstants.LEFT);
		labDate.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		labDate.setBounds(301, 107, 206, 38);
		add(labDate);
		
		JLabel label_4 = new JLabel("进货人：");
		label_4.setHorizontalAlignment(SwingConstants.CENTER);
		label_4.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		label_4.setBounds(506, 107, 105, 38);
		add(label_4);
		
		JLabel labInUserId = new JLabel(inMain.getInUserId()+" ,"+inMain.getInUserName());
		labInUserId.setHorizontalAlignment(SwingConstants.LEFT);
		labInUserId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		labInUserId.setBounds(586, 107, 185, 38);
		add(labInUserId);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(70, 165, 656, 239);
		add(scrollPane);
		
		jList = new JList<InDetailModel>();
		scrollPane.setViewportView(jList);
		jList.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setForeground(Color.BLUE);
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(336, 437, 113, 45);
		add(btnBack);
	}
}
